rmgr's wiki

> / > general > interesting-links

6502 SPI SD Card

https://www.rehsdonline.com/post/calling-all-spi-sd-card-wizards-troubleshooting-sd-card-access-with-spi

Description

Full data reading is now working! Progress Updates posted at the bottom... I am currently trying to get an SD Card reader working with my 6502 system. I have plenty of research and experimenting to do yet, but I am posting this in case anyone catches something obvious that I'm missing. Short version: I am getting results of 05 when making many of the SPI-based calls to the SD Card reader. To the right are the readers and SD Cards that I am using. The readers are HiLetgo MicroSD Card readers. I h

Content

[rehsd](https://www.rehsdonline.com/home) {#rehsd .font_0 style="line-height:1.1em; font-size:51px;"}

=========================================

{srcset="https://static.wixstatic.com/media/41d000_273d87e05f3236a242711250078dfd6d.png/v1/fill/w_143,h_70,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/41d000_273d87e05f3236a242711250078dfd6d.png 1x, https://static.wixstatic.com/media/41d000_273d87e05f3236a242711250078dfd6d.png/v1/fill/w_286,h_140,al_c,lg_1,q_85,enc_auto/41d000_273d87e05f3236a242711250078dfd6d.png 2x"}

- #DrpDwnMn00}

HOME

- #DrpDwnMn01}

PROJECTS

- #DrpDwnMn02}

ABOUT

- #DrpDwnMn03}

CONTACT

- #DrpDwnMn04}

BLOG

- #DrpDwnMn05}

RESOURCES

- ::: {#DrpDwnMn0__more__}

More

:::

```{=html}

!-- --

```

Use tab to navigate through the menu items.

Log In

- [All Posts](https://www.rehsdonline.com/blog){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color .has-custom-focus .ckIWcX}

- [x86](https://www.rehsdonline.com/blog/categories/x86){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [16-bit CPU](https://www.rehsdonline.com/blog/categories/16-bit-cpu){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [65816](https://www.rehsdonline.com/blog/categories/65816-1){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [6502](https://www.rehsdonline.com/blog/categories/6502){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [8-bit CPU](https://www.rehsdonline.com/blog/categories/8-bit-cpu){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [Audio](https://www.rehsdonline.com/blog/categories/audio){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [Commodore](https://www.rehsdonline.com/blog/categories/commodore){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [FPGA](https://www.rehsdonline.com/blog/categories/fpga){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [Graphics](https://www.rehsdonline.com/blog/categories/graphics){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [Other Peripherals](https://www.rehsdonline.com/blog/categories/other-peripherals){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color}

- [Software](https://www.rehsdonline.com/blog/categories/software){.UlUPo4 .blog-navigation-container-color .blog-navigation-container-font .blog-navigation-link-hover-color .has-custom-focus .ckIWcX}

Search

{.lR3yj5 .fluid-avatar-image}rehsd

- - Jan 4

-

- 6 min read

Calling All SPI SD Card Wizards: Troubleshooting SD Card Access with SPI {#calling-all-spi-sd-card-wizards-troubleshooting-sd-card-access-with-spi .UbhFJ7 .nkqC0Q .blog-post-title-font .blog-post-title-color .blog-text-color .post-title .blog-hover-container-element-color .FG3qXk .blog-post-page-title-font hook="post-title"}

========================================================================

Updated: Feb 11

\

I am currently trying to get an SD Card reader working with my 6502 system. I have plenty of research and experimenting to do yet, but I am posting this in case anyone catches something obvious that I\'m missing. **Short version**: I am getting results of **05** when making many of the SPI-based calls to the SD Card reader.

To the right are the readers and SD Cards that I am using. The readers are

\

For 6502 assembly code, I started with a posting from George Foot, titled [*[Reading SD Cards on a 6502+6522 Computer]{.underline}*](https://hackaday.io/project/174867-reading-sd-cards-on-a-65026522-computer){._3Bkfb ._1lsz7}. Using his code as a starting point, I created

1. ::: {#viewer-6isvs}

Starts the card initialization process by cycling the SPI clock for 80+ full clock cycles with MOSI high.

:::

2. ::: {#viewer-ablsh}

With CS set for the SD Card reader, sends **CMD0**. Expecting result of 01. I typically get expected results from this (for card \#1 or card \#2).

:::

3. ::: {#viewer-9ccq0}

Sends **CMD8**. Expecting result of 01, unless the card is really old. I receive **05**.

:::

4. ::: {#viewer-29n9r}

Send **CMD55** (expecting back 01), followed by **CMD41** (expecting back 00). Again, I get **05** as a result for many of these calls.

:::

5. ::: {#viewer-48ltu}

Regardless of errors, I attempt to read a sector (of course, with no luck).

:::

Examples of sequencing of commands and specific values being sent and received are below. This is debug output captured on my 6502 VGA.

Debug Info for SD Card 1 Communication

Debug Info for SD Card 1 Communication

Debug Info for SD Card 2 Communication

Debug Info for SD Card 2 Communication

Debug Info for SD Card 3 Communication

Debug Info for SD Card 3 Communication

As seen below, I am connecting the SD Card reader to my SPI card. The SPI card has a series of OEB/CS connections for different SPI devices. Connections for the SPI clock, MOSI, and MISO are also provided from the card pin headers. The SD Card is connected to 5V power (the reader has a built-in 3.3V level shifter, as I understand it).

Connection from SD Card Reader to SPI Card

Connection from SD Card Reader to SPI Card

\

The SD Cards are formatted with either FAT32 or FAT, based on their capacity. Each card contains two files with six characters in each file.

Card Format and Contents

Card Format and Contents

\

As far as I can tell, the SPI communication looks OK. Below are some quick captures. These screen captures were from a test with SD Card \#3 showing the initial clock cycling (for initialization), sending CMD0, and receiving an incorrect response of C0 (not 01). This first command, CMD0, works fine on both of the other SD Cards. I have been validating with the scope that the values I believe I am sending and receiving from 6502 assembly code are actually seen on the wire.

\

According to

\

I saw in

CS/OE Toggled with Each Command

CS/OE Toggled with Each Command

\

\

ext Steps {#viewer-bmr6o ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

- ::: {#viewer-c3hmt}

Google, Google, Google :) \--reading other tutorials, references, data sheets to see what I might be missing.

:::

- ::: {#viewer-33clk}

Connect an SD Card reader to an Arduino and use existing Arduino libraries to test. I can then review the code to see how communication is being handled. I can also use a scope and/or logic analyzer to see how the Arduino code might approach the communication differently from what I\'m doing.

:::

- ::: {#viewer-skb4}

**Other suggestions?**

:::

{#viewer-4dc3j ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}
eferences I am Using {#viewer-1qr0b ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

- ::: {#viewer-9ouue}

:::

- ::: {#viewer-3uim6}

:::

- ::: {#viewer-bmr04}

:::

- ::: {#viewer-1g6t9}

:::

\

pdates {#viewer-v9lj ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

- ::: {#viewer-2imia}

:::

- ::: {#viewer-f26hv}

With my 32GB card, I get stuck on CMD0 with a return of C0, indicating some kind of issue, possibly a voltage/shift issue. I have a second, identical card; I get the same results.

:::

- ::: {#viewer-78jnb}

With my 1GB card, I get past CMD0 but then get 05 on CMD8. I believe this is correct behavior, as this is an older card (non-HC). I will need to try CMD1 on this card.

:::

- ::: {#viewer-4mm24}

With regard to the 55+41 sequence problem, it seems like I\'m getting an early response from the SD Card during my CM41 command. It\'s like it thinks it\'s received enough command packets to send a response (click the image below for a larger view).

:::

- ::: {#viewer-46j84}

SPI Card schematic:

:::

- ::: {#viewer-e9coq}

:::

- ::: {#viewer-9m616}

I have only tested my 8GB card so far. I am curious to see if my 32GB card will now work. I still need to modify the initialization to support my older 1GB card.

:::

- ::: {#viewer-2l5q3}

Thanks again to

:::

- ::: {#viewer-f205h}

The following video shows current state. I have not yet started work on actually reading data from the SD Card, so disregard the last bits that show data being read (I don\'t know if it\'s valid yet or not).

:::

```{=html}

!-- --

```

- ::: {#viewer-2bi5p}

I have updated the

:::

- ::: {#viewer-17ji9}

In the process of getting this to work, I have identified some room for improvement with my SPI Card, lol. The biggest thing is the default values for CS/OE and other shift register control signals. I should probably invert the signals going into many of these. This would allow me to cleanup my 6502 code so that I don\'t have to constantly include the flag to keep a signal high and only include the flag when I really need that signal changed. In other words, set the flag 20% of the time, not 80% of the time. The other issue is having easier control of MOSI. Since I have the signal for MOSI coming out of a shift register, I have to deal with the shift register to simply bring MOSI high (say, for 8 SPI clock cycles). Maybe an OR gate with the shift register output and a VIA output would provide a handy way to manually bring up the value of MOSI without messing with the shift register.

:::

- ::: {#viewer-ecb0l}

With regard to the SPI Card, I might also be creating far more work than it\'s worth. Just using a VIA for SPI communication might actually be easier. I\'m OK with this, since I wanted to learn shift registers. I can tell you that the SPI Card has helped me do that. :)

:::

\

elpful Tools for Troubleshooting {#viewer-3kuu7 ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

Troubleshooting SPI and SD Card communication can be a bit tedious (and frustrating) at times. A few things that helped me maintain my sanity:

1. ::: {#viewer-4mll7}

The high-resolution VGA output on my 6502 allowed me to print plenty of debug data to the screen. While possible with my 6502 onboard two-line LCD, that would have been quite painful. A quarter of the VGA screen can been seen in the video above.

:::

2. ::: {#viewer-bfmkg}

My oscilloscope is able to interpret SPI. I found this very helpful when getting basic SPI communication started with the SD Card. Once I got into long sequences of SPI communication before seeing a failure, the oscilloscope was not as helpful.

:::

3. ::: {#viewer-8jtdi}

Finally, I used a logic analyzer to debug the longer SPI sequences. My specific analyzer is a

:::

ext Steps {#viewer-5ftni ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

While I have the initialization working, I still need to actually read data from the SD Card. :)

\

ostscript {#viewer-f0fk9 ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

I have been able to test the above initialization code with multiple SD Cards, including the 1GB, 8GB, and 32GB ones shown earlier. All three are initializing fine, which is great to see!

\

Now I need to get code in place to read data. Mark Hindsbo has a nice

\

Reading raw data:

PS {#viewer-1gne4 ._2TrLF ._1j-51 ._1FoOD ._3M0Fe .Z63qyL .roLFQS .public-DraftStyleDefault-block-depth0 .fixed-tab-size .public-DraftStyleDefault-text-ltr}

I can now effectively read data on the SD Card, with no file system in play. Essentially, I can treat the SD Card as one big data storage ROM. Now, I can go back to

\

\

0 views0 comments

Post not marked as liked